home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr27 / djlist.zip / DJLIST.C next >
Text File  |  1993-05-16  |  4KB  |  214 lines

  1. /* program to produce side-by-side listings of programs and manuals, etc. *
  2.  * on an HP DeskJet+, a la the PD utility laserlst.com, which appeared in *
  3.  * PC Mag some time ago.                                                  *
  4.  *                                                                        *
  5.  * (c) Adam E. Saltman, 1989                                              *
  6.  *                                                                        */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <ctype.h>
  12. #include <sys\stat.h>
  13.  
  14. #define ESC        0x1b
  15. #define LANDSCAPE "&l1O"
  16. #define DRAFTMODE "(s1Q"
  17. #define EIGHTLPI  "&l8D"
  18. #define TWENTYCPI "(s20H"
  19. #define RESET     'E'
  20. #define FORMFEED   0x0c
  21. #define RETURN     '\n'
  22. #define LEFT       1
  23. #define RIGHT      2
  24. #define MIDDLE     100
  25.  
  26. char page[58][201];
  27.  
  28. void
  29. blankpage(void)
  30. {
  31.   int i, j;
  32.  
  33.   for (i=0; i<57; i++)
  34.   {
  35.     for (j=0; j<200; j++)
  36.       page[i][j] = ' ';
  37.     page[i][j] = 0;
  38.   }
  39. }
  40.  
  41. void
  42. printpage(void)
  43. {
  44.   int i;
  45.  
  46.   for (i=0; i<57; i++)
  47.   {
  48.     fputs(page[i], stdout);
  49.     fputc(RETURN, stdout);
  50.   }
  51.  
  52.   fputc(FORMFEED, stdout);
  53.  
  54.   blankpage();
  55. }
  56.  
  57. void
  58. main(int argc, char **argv)
  59. {
  60.   /* parse options here, eventually */
  61.  
  62.   int line,
  63.       column,
  64.       columnOffset,
  65.       pageNumber,
  66.       side,
  67.       tabToSpaces = 4,
  68.       endOfFile;
  69.  
  70.   long numChars;
  71.  
  72.   FILE *fp;
  73.  
  74.   struct stat statbuf;
  75.  
  76.   fp = fopen(argv[1], "rt");
  77.   if (fp == NULL)
  78.   {
  79.     printf("Cannot open %s\n", argv[1]);
  80.     exit(-1);
  81.   }
  82.  
  83.   stat(argv[1], &statbuf);
  84.  
  85.   fputc(ESC, stdout);
  86.   fputs(LANDSCAPE, stdout);
  87.  
  88.   fputc(ESC, stdout);
  89.   fputs(EIGHTLPI, stdout);
  90.  
  91.   fputc(ESC, stdout);
  92.   fputs(TWENTYCPI, stdout);
  93.  
  94.   fputc(ESC, stdout);
  95.   fputs(DRAFTMODE, stdout);
  96.  
  97.   blankpage();
  98.  
  99.   line = 3;
  100.   column = 0;
  101.   columnOffset = 0;
  102.  
  103.   pageNumber = 1;
  104.   side = LEFT;
  105.  
  106.   numChars = 0L;
  107.   endOfFile = 0;
  108.  
  109.   for(;;)
  110.   {
  111.     int c;
  112.  
  113.     c = fgetc(fp);
  114.  
  115.     numChars++;
  116.  
  117.     if (numChars == statbuf.st_size)
  118.       endOfFile = 1;
  119.  
  120.     if ((c == '\n') || (line >= 58) || endOfFile)
  121.     {
  122.       if ((line >= 58) || endOfFile)
  123.       {
  124.         char buf[4];
  125.  
  126.         itoa(pageNumber, buf, 10);
  127.  
  128.         if (side == LEFT)
  129.         {
  130.           int i;
  131.  
  132.           strncpy(&page[0][0], argv[1], strlen(argv[1]));
  133.           strncpy(&page[0][MIDDLE - strlen(buf) - 9], "Page ", 5);
  134.           strncpy(&page[0][MIDDLE - strlen(buf) - 4], buf, strlen(buf));
  135.  
  136.           for (i=0; i<58; i++)
  137.             page[i][MIDDLE] = '|';
  138.  
  139.           side = RIGHT;
  140.           columnOffset = MIDDLE + 5;
  141.  
  142.           if (endOfFile)
  143.           {
  144.             printpage();
  145.             fclose(fp);
  146.             fputc(ESC, stdout);
  147.             fputc(RESET, stdout);
  148.             exit(0);
  149.           }
  150.         }
  151.  
  152.         else
  153.         {
  154.           strncpy(&page[0][columnOffset], argv[1], strlen(argv[1]));
  155.           strncpy(&page[0][columnOffset + 84], "Page ", 5);
  156.           strncpy(&page[0][columnOffset + 89], buf, strlen(buf));
  157.  
  158.           printpage();
  159.  
  160.           if (endOfFile)
  161.           {
  162.             fclose(fp);
  163.             fputc(ESC, stdout);
  164.             fputc(RESET, stdout);
  165.             exit(0);
  166.           }
  167.  
  168.           side = LEFT;
  169.           columnOffset = 0;
  170.         }
  171.  
  172.         line = 3;
  173.         column = 0;
  174.         pageNumber++;
  175.  
  176.         if (isprint(c))
  177.           page[line][column++] = c;
  178.       }
  179.  
  180.       else
  181.       {
  182.         line++;
  183.         column = 0;
  184.       }
  185.  
  186.       continue;
  187.     }
  188.  
  189.     if (c == '\t')
  190.     {
  191.       int i;
  192.  
  193.       for (i = 0; i < tabToSpaces; i++)
  194.       {
  195.         page[line][column + columnOffset + i] = ' ';
  196.         if (column >= MIDDLE - 5)
  197.           break;
  198.         column++;
  199.       }
  200.  
  201.       continue;
  202.     }
  203.  
  204.     page[line][column + columnOffset] = (char)c;
  205.     column++;
  206.  
  207.     if (column >= MIDDLE - 5)
  208.     {
  209.       column = 0;
  210.       line++;
  211.     }
  212.   }
  213. }
  214.